package com.congiya.youtube.activity;

import java.util.ArrayList;

import android.app.ExpandableListActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;

import com.congiya.youtube.R;
import com.congiya.youtube.db.SqlLiteDBUtil;

public class FolderList extends ExpandableListActivity {

	private static String LOG_TAG = "FolderList";

	ExpandableListAdapter mAdapter;
	SqlLiteDBUtil dbUtil;
	Menu mMenu;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		dbUtil = new SqlLiteDBUtil();

		mAdapter = new MyExpandableListAdapter();
		setListAdapter(mAdapter);
		registerForContextMenu(getExpandableListView());

	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {

		menu.setHeaderTitle("Sample menu");
		menu.add(0, 0, 0, "sample ....");
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		mMenu = menu;
		// Inflate the currently selected menu XML resource.
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.folder_menu, menu);// xml menu
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {

		Log.d(LOG_TAG, "");

		switch (item.getItemId()) {

			case R.id.reg :
				Toast.makeText(this, "폴더 추가 중비중!!!", Toast.LENGTH_SHORT).show();
				return true;

		}

		return false;
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {

		ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item
				.getMenuInfo();

		String title = ((TextView) info.targetView).getText().toString();

		int type = ExpandableListView
				.getPackedPositionType(info.packedPosition);
		if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
			int groupPos = ExpandableListView
					.getPackedPositionGroup(info.packedPosition);
			int childPos = ExpandableListView
					.getPackedPositionChild(info.packedPosition);
			Toast.makeText(
					this,
					title + ": Child " + childPos + " clicked in group "
							+ groupPos, Toast.LENGTH_SHORT).show();
			return true;
		} else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
			int groupPos = ExpandableListView
					.getPackedPositionGroup(info.packedPosition);
			Toast.makeText(this, title + ": Group " + groupPos + " clicked",
					Toast.LENGTH_SHORT).show();
			return true;
		}

		return false;
	}

	private class MyExpandableListAdapter extends BaseExpandableListAdapter {

		private Object MY_FOLDERS_STEP1_TITLE[];
		private Object MY_FOLDERS_STEP1_SUBCNT[];
		private String MY_FOLDERS_STEP2[][];

		public MyExpandableListAdapter() {

			//ArrayList tmpTopList = dbUtil.getTopMenuList();
//			MY_FOLDERS_STEP1_TITLE = (String[]) tmpTopList.get(0);
//			MY_FOLDERS_STEP1_SUBCNT = (String[]) tmpTopList.get(1);
			
			MY_FOLDERS_STEP1_TITLE = (String[]) dbUtil.getTopMenuList();
			MY_FOLDERS_STEP1_SUBCNT = new String[] {"2", "2"};
			
			MY_FOLDERS_STEP2 = dbUtil.getSubMenuList(2,
					MY_FOLDERS_STEP1_TITLE.length);

			// Log.i(LOG_TAG, "MyExpandableListAdapter.TOP LEN : " +
			// MY_FOLDERS_STEP1_TITLE.length);

		}

		public Object getChild(int groupPosition, int childPosition) {

			// Log.i(LOG_TAG, "getChild : " + groupPosition + " : " +
			// childPosition);
			return MY_FOLDERS_STEP2[groupPosition][childPosition];
		}

		public long getChildId(int groupPosition, int childPosition) {

			// Log.i(LOG_TAG, "MyExpandableListAdapter.getChildId : " +
			// groupPosition + " : " + childPosition);
			return childPosition;
		}

		public int getChildrenCount(int groupPosition) {

			// Log.i(LOG_TAG, "getChildrenCount : " + groupPosition + " : " +
			// (String)MY_FOLDERS_STEP1_SUBCNT[groupPosition]);
			return Integer
					.parseInt((String) MY_FOLDERS_STEP1_SUBCNT[groupPosition]);
		}

		public TextView getGroupView(int groupPosition) {
			// Layout parameters for the ExpandableListView
			// AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
			// ViewGroup.LayoutParams.MATCH_PARENT, 50);

			AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
					ViewGroup.LayoutParams.FILL_PARENT, 50);

			TextView textView = new TextView(FolderList.this);
			textView.setLayoutParams(lp);

			// Center the text vertically
			textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);

			// Set the text starting position
			textView.setPadding(36, 0, 0, 0);

			if (groupPosition % 2 == 0)
				textView.setBackgroundColor(Color.DKGRAY);
			else
				textView.setBackgroundColor(Color.GRAY);

			return textView;
		}
		public TextView getItemView() {
			// Layout parameters for the ExpandableListView
//			AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
//					ViewGroup.LayoutParams.MATCH_PARENT, 40);
			
			AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
					ViewGroup.LayoutParams.FILL_PARENT, 40);

			TextView textView = new TextView(FolderList.this);
			textView.setLayoutParams(lp);

			// Center the text vertically
			textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);

			// Set the text starting position
			textView.setPadding(36, 0, 0, 0);
			// textView.setBackgroundColor(Color.LTGRAY);

			return textView;
		}

		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {

			TextView textView = getItemView();

			// Log.i(LOG_TAG, "getItemView["
			// +groupPosition+"]["+childPosition+"]" + textView.getText());
			textView.setText((String) getChild(groupPosition, childPosition));
			return textView;
		}

		public Object getGroup(int groupPosition) {

			return MY_FOLDERS_STEP1_TITLE[groupPosition];
		}

		public int getGroupCount() {
			return MY_FOLDERS_STEP1_TITLE.length;
		}

		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {

			TextView textView = getGroupView(groupPosition);
			textView.setText(getGroup(groupPosition).toString());
			return textView;
		}

		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}

		public boolean hasStableIds() {
			return true;
		}

	}

}
